package com.etheart.lyric.htmlparser.service.jujing;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.springframework.stereotype.Service;

import com.etheart.framework.core.util.RegexUtils;
import com.etheart.lyric.htmlparser.util.HtmlparserUtils;

@Service("htmlparse_JujingAlbumListParseService")
public class JujingAlbumListParseService {
	 
	public List<String> parseAlbumListPage(Parser parser,String path,String url) throws ParserException{
		//name  http://www.top100.cn/album/list.php?singerid=650&pages=1
		parser.reset();
		int pageNum  = getAlbumPageNum(parser,path);
		List<String> albumUrlList = new ArrayList<String>(10);
		
		for (int j = 1; j <= pageNum; j++) {
			if (j != 1) {
				String filePath = path.replace(".php", "")+"&pages="+j+".php";
				parser = HtmlparserUtils.getParser(filePath,url+"&pages="+j);
				if (parser==null) {
					logger.error("albumPageGetErr:"+filePath);
					continue;
				}
			}
			parser.reset();
			NodeFilter nodeFilter = new AndFilter(new TagNameFilter("a"),new HasAttributeFilter("class", "Listen_point right"));
			NodeList nodeList = parser.parse(nodeFilter);
			for (int i = 0; i < nodeList.size(); i++) {
				Node node = nodeList.elementAt(i);
				//  <a href="/album/index-skcadnpbage3dqcbogy.shtml"  class="Listen_point right" style="width:50px;" target="_blank">等多</a>    
				String href = getAlbumUrlFromA(node.toHtml());
				albumUrlList.add(href);
			}
		}
		return albumUrlList;
	}

	

	private int getAlbumPageNum(Parser parser,String id) {
		//<span style="margin-left:10px; font-weight:bold">张惠妹（共有26张专辑）</span>
		parser.reset();
		NodeFilter nodeFilter = new AndFilter(new TagNameFilter("span"),new HasAttributeFilter("style", "margin-left:10px; font-weight:bold"));
		NodeList nodeList;
		try {
			nodeList = parser.parse(nodeFilter);
			Node node = nodeList.elementAt(0);
			String num = RegexUtils.getMatcheGroup(".*共有([0-9]*)张专辑.*", node.toHtml());
			int n=Integer.parseInt(num);
			return n%10==0? n/10: n/10+1;
		} catch (ParserException e) {
			logger.error("专辑数parseErr，id=("+id+")");
		}
		return 0;
	}
	private static final Log logger = LogFactory
	.getLog(JujingAlbumListParseService.class);
	
	private String getAlbumUrlFromA(String a) {
          return RegexUtils.getMatcheGroup(".*href=\"([^\"]*)\".*", a);
	}

	public String parseTitle(File file, String fileEncode, String charset,
			String baseUrl) throws ParserException, IOException {
		String fileString = FileUtils.readFileToString(file, fileEncode);
		return parseTitle(fileString,charset);
	}

	private String parseTitle(String html, String charset) throws ParserException {
		Parser parser = Parser.createParser(html, charset);
		
			NodeFilter nodeFilter = new TagNameFilter("title");
			NodeList nodeList = parser.parse(nodeFilter);
			Tag tag = (Tag) nodeList.elementAt(0);
			return tag.toPlainTextString();
			
	}
	
	
}
